#include <SPI.h>
#include <WiFi.h>

char ssid[] = "ICE409";     //your network SSID (name) 
char pass[] = "409ice409"; //your network password

int status = WL_IDLE_STATUS;
IPAddress server(220,67,124,22);
WiFiClient client;

bool connected = false;
bool check = false;
String trans = "";

int flexpin1 = 0; 
int flexpin2 = 1;
int flexpin3 = 2;

int melody[] = {
  262, 294, 330, 349, 392, 440, 494, 523};

void setup(){
  Serial.begin(9600); 
  Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");
  Serial.println(ssid);
}

void loop(){
  status = WiFi.begin(ssid, pass);
  
  if ( status != WL_CONNECTED) { 
    Serial.println("Couldn't get a wifi connection");
    //don't do anything else:
    while(true);
  } 
  else {
    //Serial.println("Connected to wifi");
    //Serial.println("\nStarting connection...");    
  } 
       
  if(!connected) {
    Serial.println("connecting............");
    connected = true;
    
  if (client.connect(server, 80)) {
      Serial.println("Socket connected");
       //#################################################################
       int flexVal1; 
       int flexVal2;
       int flexVal3;

       flexVal1 = analogRead(flexpin1); 
       flexVal2 = analogRead(flexpin2);
       flexVal3 = analogRead(flexpin3);

       Serial.print("sensor1: "); 
       Serial.println(flexVal1); 

       Serial.print("sensor2: ");
       Serial.println(flexVal2); 

       Serial.print("sensor3: ");
       Serial.println(flexVal3); 
       Serial.println();
  
       if(flexVal1 >800) {
         check = true;
         tone(8, melody[0]);  // 'do' sound
         trans = "do";         
         //delay(500);
         noTone(8);    
       } else if(flexVal2 >800) {
         check = true;
         tone(8, melody[1]);  //'re' sound
         trans = "re";
         //delay(500);
         noTone(8);     
       } else if(flexVal3 >800) {
         check = true;
         tone(8, melody[2]);  //'mi' sound
         trans = "mi";
         //delay(500);
         noTone(8);        
       }
       
       if(check) {
         client.print("GET /insert.php?val=");
         client.print(trans);
         client.println(" HTTP/1.1");
         client.println("Host: 220.67.124.22");
        
         client.println("Connection: close");
         client.println();
       }
       check = false;       
       delay(300);
      //################################################################    
      //delay(500);       
   }else {
    Serial.println("Socket connection failed");
   } 
  } else {
    if(client.connected() && client.available()) {
      char c = client.read();
      Serial.print(c);
  }
  
  //Serial.println("disconnecting");
  client.stop();
  connected = false;
 }
}
